package com.zhouyibike.uu.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.zhouyibike.uu.R;
import com.zhouyibike.uu.entity.HehuorenxuanzejiemianResult;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.List;

/**
 * Created by cc on 2017/5/3.
 */

public class SelectHehuorenAdapter extends BaseAdapter {
    private Context context;
    private List<HehuorenxuanzejiemianResult> list;
    final int TYPE_1 = 0;

    public SelectHehuorenAdapter(Context context, List<HehuorenxuanzejiemianResult> list) {
        this.context = context;
        this.list = list;
    }

    public void setList(List<HehuorenxuanzejiemianResult> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
//        return position == list.size() - 1 ? TYPE_2 : TYPE_1;
        return TYPE_1;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        int type = getItemViewType(position);
        if (convertView == null) {
            switch (type) {
                case TYPE_1:
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_checkbox, null);
                    holder = new ViewHolder();
                    // 以下为保存这一屏的内容，供下次回到这一屏的时候直接refresh，而不用重读布局文件
                    holder.img = (CheckBox) convertView.findViewById(R.id.iv_item_ck);
                    holder.houtainame = (TextView) convertView.findViewById(R.id.item_cityjibie);
                    holder.shouyitype = (TagFlowLayout) convertView.findViewById(R.id.item_cityshouyitype);
                    holder.xiangqing = (TextView) convertView.findViewById(R.id.item_hehuorenxiangqing);
                    convertView.setTag(holder);
                    break;
            }
        } else {
            switch (type) {
                case TYPE_1:
                    holder = (ViewHolder) convertView.getTag();
                    break;
            }
        }
//        holder.img.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                if (list.get(position).ischeck)
//                {
//                    list.get(position).ischeck=true;
//                    notifyDataSetChanged();
//                }else{
//                    for (int i = 0; i <list.size() ; i++) {
//                        list.get(i).ischeck=false;
//                    }
//                    list.get(position).ischeck=true;
//                    notifyDataSetChanged();
//                }
//            }
//        });
        holder.houtainame.setText(list.get(position).itemname);
        if (list.get(position).ischeck) {
            holder.img.setChecked(true);
        } else {
            holder.img.setChecked(false);
        }
        holder.xiangqing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (position == 0) {
                    Toast.makeText(context, "省详情", Toast.LENGTH_SHORT).show();
                } else if (position == 1) {
                    Toast.makeText(context, "市详情", Toast.LENGTH_SHORT).show();
                } else if (position == 2) {
                    Toast.makeText(context, "区详情", Toast.LENGTH_SHORT).show();
                } else if (position == 3) {
                    Toast.makeText(context, "个人详情", Toast.LENGTH_SHORT).show();
                }
            }
        });
        TagAdapter<String> adapter = new TagAdapter<String>(list.get(position).itemhavegoodlist) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = (TextView) View.inflate(context, R.layout.tv, null);
                tv.setText(s);
                return tv;
            }
        };
        holder.shouyitype.setAdapter(adapter);
        return convertView;
    }

    public final class ViewHolder {
        public CheckBox img;
        public TextView xiangqing;
        public TagFlowLayout shouyitype;
        public TextView houtainame;
    }

}
